/**
 * Created by Yu Jin Xian on 2016/12/15.
 */
function primiviteTypes() {
    var s1 = 'some text';
    var s2 = s1.substring(2);

    function background() {
        var s1 = new String('some text');
        var s2 = s1.substring(20);
        s1 = null;
    }

    function lifetime() {
        var s1 = 'some text';
        s1.color = 'red';
        alert(s1.color);                            // undefined
    }

    function factory() {
        var obj = new Object('some text');
        alert(obj instanceof String);                   // true 
    }

    function convert() {
        var value = '25';
        var number = Number(value);                     // 转型函数
        alert(typeof number);                           // "number"

        var obj = new Number(value);                    // 构造函数
        alert(typeof obj);                              // "object"
    }
}

function booleanTypes() {
    var booleanObject = new Boolean(true);

    var falseObject = new Boolean(false);
    var result = falseObject && true;
    alert(result);                                  // true

    var falseValue = false;
    result = falseValue && true;
    alert(result);                                  // false

    alert(typeof falseObject);                      // object
    alert(typeof falseValue);                       // boolean
    alert(falseObject instanceof Boolean);          // true
    alert(falseValue instanceof Boolean);           // false
}

function numberTypes() {
    var numberObject = new Number(10);

    var num = 10;
    alert(num.toString());                          // "10"
    alert(num.toString(2));                         // "1010"
    alert(num.toString(8));                         // "10"
    alert(num.toString(16));                        // "a"

    function toFixed() {
        var num = 10;
        alert(num.toFixed(2));                      // "10.00"

        function decimalPlaces2() {
            var num = 10.005;
            alert(num.toFixed(2));                      // "10.01"
        }
    }

    function toExponential() {
        var num = 10;
        alert(num.toExponential(1));                // "1.0e+1"

        function auto() {
            var num = 99;
            alert(num.toExponential(1));                // "1e+1"
            alert(num.toExponential(2));                // "99"
            alert(num.toExponential(3));                // "99.0"
        }
    }

    function determineTypes() {
        var numberObject = new Number(10);
        var numberValue = 10;
        alert(typeof numberObject);
        alert(typeof numberValue);
        alert(numberObject instanceof Number);
        alert(numberValue instanceof Number);
    }
}

function stringTypes() {
    var stringObject = new String('hello world');
    var stringValue = 'hello world';
    alert(stringValue.length);                      // "11"

    function charAt() {
        var stringValue = 'hello world';
        alert(stringValue.charAt(1));               // "e"
    }

    function charCodeAt() {
        var stringValue = 'hello world';
        alert(stringValue.charCodeAt(1));           // "101"
    }

    function index() {
        var stringValue = 'hello world';
        alert(stringValue[1]);                      // "e"
    }

    function concat() {
        var stringValue = 'hello ';
        var result = stringValue.concat('world');
        alert(result);                              // "hello world"
        alert(stringValue);                         // "hello"

        function multiParameters() {
            var stringValue = 'hello ';
            var result = stringValue.concat('world', '!');
            alert(result);                          // "hello world!"
            alert(stringValue);                     // "hello"
        }
    }

    function manipulationMethods() {
        var stringValue = 'hello world';
        alert(stringValue.slice(3));                // "lo world"
        alert(stringValue.substring(3));            // "lo world"
        alert(stringValue.substr(3));               // "lo world"
        alert(stringValue.slice(3, 7));             // "lo w"
        alert(stringValue.substring(3, 7));         // "lo w"
        alert(stringValue.substr(3, 7));            // "lo worl"

        function negative() {
            var stringValue = 'hello world';
            alert(stringValue.slice(-3));            // "rld"
            alert(stringValue.substring(-3));        // "hello world"
            alert(stringValue.substr(-3));           // "rld"
            alert(stringValue.slice(3, -4));         // "lo w"
            alert(stringValue.substring(3, -4));     // "hel"
            alert(stringValue.substr(3, -4));        // ""
        }
    }

    function locationMethods() {
        var stringValue = 'hello world';
        alert(stringValue.indexOf('o'));            // 4
        alert(stringValue.lastIndexOf('o'));        // 7

        function secondParameter() {
            var stringValue = 'hello world';
            alert(stringValue.indexOf('o', 6));     // 7
            alert(stringValue.lastIndexOf('o', 6)); // 4
        }

        function search() {
            var stringValue = 'Loerm ipsum dolor sit amet, consectertur adipisicing elit';
            var postions = new Array();
            var pos = stringValue.indexOf('e');
            while (pos > -1) {
                postions.push(pos);
                pos = stringValue.indexOf('e', pos + 1);
            }
            alert(postions);                        // "3,24,32,35,52"
        }
    }

    function trim() {
        var stringValue = '    hello world    ';
        var trimmedStringValue = stringValue.trim();
        alert(stringValue);                         // "    hello world    "
        alert(trimmedStringValue);                  // "hello world"
    }

    function caseMethods() {
        var stringValue = 'hello world';
        alert(stringValue.toLocaleUpperCase());     // "HELLO WORLD"
        alert(stringValue.toUpperCase());           // "HELLO WORLD"
        alert(stringValue.toLocaleLowerCase());     // "hello world"
        alert(stringValue.toLowerCase());           // "hello world"
    }

    function patternMatching() {
        function match() {
            var text = 'cat, bat, sat, fat';
            var pattern = /.at/;
            var matches = text.match(pattern);      // 与 pattern.exec(text) 相同
            alert(matches.index);                   // 0
            alert(matches[0]);                      // "cat"
            alert(pattern.lastIndex);               // 0
        }

        function search() {
            var text = 'cat, bat, sat, fat';
            var pos = text.search(/at/);
            alert(pos);                             // 1
        }

        function replace() {
            var text = 'cat, bat, sat, fat';
            var result = text.replace('at', 'ond');
            alert(result);                          // "cond, bond, sond, fond"

            result = text.replace(/at/g, 'ond');
            alert(result);                          // "cond, bond, sond, font"

            function $() {
                var text = 'cat, bat, sat, fat';
                var result = text.replace(/(.at)/g, 'word ($1)');
                alert(result);                      // "word (cat), word (bat), word (sat), word (fat)"
            }

            function htmlEscape(text) {
                return text.replace(/[<>"&]/g, function (match, postion, originalText) {
                    switch (match) {
                        case '<':
                            return '&lt;';
                        case '>':
                            return '&gt;';
                        case '&:':
                            return '&amp;';
                        case '"':
                            return '&quot;';
                    }
                });
            }

            alert(htmlEscape('<p class="greeting">Hello world!</p>'));
            // "&lt;p calss=&quot;greeting&quot;&gt;Hello world!&lt;/&qt;"
        }

        function split() {
            var colorText = 'red,blue,green,yellow';
            var colors1 = colorText.split(',');
            var colors2 = colorText.split(',', 2);
            var colors3 = colorText.split(/[^\,]+/);
        }

        function localeCompare() {
            var stringValue = 'yellow';
            alert(stringValue.localeCompare('brick'));  // 1
            alert(stringValue.localeCompare('yellow')); // 0
            alert(stringValue.localeCompare('zoo'));    // -1

            function determineOrder(value) {
                var result = stringValue.localeCompare(value);
                if (result < 0) {
                    alert('The string "yellow" comes before the string "' + value + '".');
                } else if (result > 0) {
                    alert('The string "yellow" comes after the string "' + value + '".');
                } else {
                    alert('The string "yellow" is equal to the string "' + value + '".');
                }
            }

            determineOrder('brick');
            determineOrder('yellow');
            determineOrder('zoo');
        }

        function fromCharCode() {
            alert(String.fromCharCode(104, 101, 108, 108, 111));    // "hello"
        }
    }
}